<template>
  <div class="publish-container">
    <el-card>
      <div slot="header" class="clearfix">
        <my-breadcrumb>编辑文章</my-breadcrumb>
      </div>
      <!-- 表单 -->
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" />
        </el-form-item>

        <el-form-item label="内容" prop="content">
          <quill-editor v-model="form.content" :options="editorOption" />
        </el-form-item>

        <el-form-item label="封面">
          <el-radio-group v-model="form.coverType" @change="hCoverTypeChange">
            <el-radio :label="1">单图</el-radio>
            <el-radio :label="3">三图</el-radio>
            <el-radio :label="0">无图</el-radio>
            <el-radio :label="-1">自动</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item v-if="form.coverType>0">
          <el-row>
            <el-col :span="6" v-for="(item,idx) in form.coverType" :key="idx">
              {{idx}}<my-cover v-model="form.coverImages[idx]"/>
            </el-col>
          </el-row>

        </el-form-item>

        <!-- 频道组件 -->
        <my-channel v-model="form.channelId"></my-channel>

        <el-form-item>
          <el-button
          :loading="isLoading"
          type="primary" @click="hSave">保存修改</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
// 导入样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
// 导入组件
import { quillEditor } from 'vue-quill-editor'

export default {
  name: 'articleAdd',
  components: {
    quillEditor
  },
  data () {
    return {
      isLoading: false,
      form: {
        title: '好文章-当我第一次看见你的时候',
        content: '当我第一次看见你的时候，我就想一直对你好',
        coverImages: [], // 保存用户选中的封面图片的地址
        coverType: 0, // 默认是无图的
        channelId: ''
      },
      rules: {
        title: [{ required: true, message: '请输入标题名称', trigger: 'blur' },
          { min: 5, max: 30, message: '长度在 5 到 30 个字符', trigger: 'blur' }],
        content: [
          { required: true, message: '请输入文章内容', trigger: 'blur' },
          { min: 20, max: 30000, message: '最少20个字', trigger: 'blur' }
        ],
        channelId: [
          { required: true, message: '请选择频道', trigger: 'blur' }
        ]
      },
      // 富文本配置对象
      editorOption: {
        // 占位配置
        placeholder: '',
        modules: {
          // 配置工具栏
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            ['blockquote', 'code-block'],
            [{ header: 1 }, { header: 2 }],
            [{ list: 'ordered' }, { list: 'bullet' }],
            [{ indent: '-1' }, { indent: '+1' }],
            ['image']
          ]
        }
      }
    }
  },
  created () {
    this.loadArticle(this.$route.params.id)
  },
  methods: {
    async loadArticle (id) {
      // 发请求，获取文章详情
      try {
        const res = await this.$axios({
          method: 'GET',
          url: '/mp/v1_0/articles/' + id
        })
        console.log(res)
        const { title, channel_id: channelId, content, cover } = res.data.data
        this.form.id = id
        this.form.title = title
        this.form.channelId = channelId
        this.form.content = content
        this.form.coverType = cover.type
        this.form.coverImages = cover.images
      } catch (err) {
        console.log(err)
      }
    },
    // 拼接参数
    buildParams () {
      const { title, content, channelId, coverType, coverImages } = this.form

      const paramsObj = {
        title: title,
        content: content,
        cover: {
          type: coverType,
          images: coverImages // 封面图片的地址
        },
        channel_id: channelId
      }
      return paramsObj
    },
    doSave () {
      // 1. 收集参数
      const paramsObj = this.buildParams()

      // 2. 按接口文档要求，去发ajax
      this.$axios({
        method: 'PUT',
        url: '/mp/v1_0/articles/' + this.form.id,
        data: paramsObj
      }).then(res => {
        this.$message.success('保存修改文章成功')
        console.log(res)
      }).catch(err => {
        console.log(err)
        this.$message.error('保存修改文章失败')
      })
    },
    // 保存修改
    hSave () {
      // 手动去做一次表单校验
      this.$refs.form.validate(valid => {
        // valid && this.doAdd(isDraft)
        if (valid) {
          this.doSave()
        }
      })
    },
    // 用户在不同的封面类型上切换
    hCoverTypeChange () {
      // console.log(1, this.form.coverType)
      if (this.form.coverType === 1) {
        // 数组:arr.length = 1; 只会保存arr[0]的值，其它会清空掉
        this.form.coverImages.length = 1
      } else if (this.form.coverType <= 0) {
        // 数组:arr.length = 1; 只会保存arr[0]的值，其它会清空掉
        this.form.coverImages.length = 0
      }
    }
  }
}
</script>

<style scoped lang='less'>
// 在父组件中去设置子组件的样式，加上/deep/ 实现穿透功能
.publish-container /deep/ .ql-editor{
  height: 300px;
}
.publish-container /deep/ .ql-toolbar.ql-snow{
  padding: 0 8px;
}
</style>
